﻿using System;
using NUnit.Framework;
using Rhino.Mocks;
using ThoughtWorks.CruiseControl.Core.Tasks.Conditions;
using ThoughtWorks.CruiseControl.Core.Util;

namespace ThoughtWorks.CruiseControl.UnitTests.Core.Tasks.Conditions
{
    [TestFixture]
    public class UrlHeaderValueTaskConditionTest
    {
        MockRepository mocks = new MockRepository();

        [TestFixtureSetUp]
        public void testFixtureSetUp()
        {
            mocks.ReplayAll();
        }

        [TestFixtureTearDown]
        public void TearDown()
        {
            mocks.VerifyAll();
        }

        delegate bool FileExistsDelegate(string url, string header, string value);
        private bool Formal(string url,string header,string value)
        {
            if (url == null) throw new ArgumentNullException("url");
            if (header == null) throw new ArgumentNullException("header");
            if (value == null) throw new ArgumentNullException("value");

            Uri uri = new Uri(url);
            if(!url.Equals("http://www.testdomain.com/default.aspx",StringComparison.InvariantCultureIgnoreCase))
                return false;
            if (!header.Equals("testKey", StringComparison.InvariantCultureIgnoreCase))
                return false;
            if(!value.Equals("testValue"))
                return false;
            return true;
        }

        [Test]
        [Description("nullUrl throwsArgumentNullException")]
        [Category("null tests")]
        [ExpectedException(typeof (ArgumentNullException))]
        public void UrlHeaderValueTaskConditionTest_nullHeaderKey_throwsArgumentNullException()
        {
            UrlHeaderValueTaskCondition condition = new UrlHeaderValueTaskCondition();
            condition.Url = "http://www.testdomain.com/default.aspx";
            condition.HeaderKey = null;
            condition.HeaderValue = "testValue";
            condition.Eval(null);
        }

        [Test]
        [Description("nullUrl throwsArgumentNullException")]
        [Category("null tests")]
        [ExpectedException(typeof (ArgumentNullException))]
        public void UrlHeaderValueTaskConditionTest_nullHeaderValue_throwsArgumentNullException()
        {
            UrlHeaderValueTaskCondition condition = new UrlHeaderValueTaskCondition();
            condition.Url = "http://www.testdomain.com/default.aspx";
            condition.HeaderKey = "testKey";
            condition.HeaderValue = null;
            condition.Eval(null);
        }

        [Test]
        [Description("nullUrl throwsArgumentNullException")]
        [Category("null tests")]
        [ExpectedException(typeof (ArgumentNullException))]
        public void UrlHeaderValueTaskConditionTest_nullUrl_throwsArgumentNullException()
        {
            UrlHeaderValueTaskCondition condition = new UrlHeaderValueTaskCondition();
            condition.Url = null;
            condition.HeaderKey = "testKey";
            condition.HeaderValue = "testValue";
            condition.Eval(null);
        }

        [Test]
        [Description("nullUrl throwsArgumentNullException")]
        [Category("null tests")]
        public void UrlHeaderValueTaskConditionTest_TestingPing_Pass()
        {
            UrlHeaderValueTaskCondition condition = new UrlHeaderValueTaskCondition();
            condition.Url = "http://www.testdomain.com/default.aspx";
            condition.HeaderKey = "testKey";
            condition.HeaderValue = "testValue";
            condition.Eval(null);
        }

    }
}